<?php
namespace app\api\model;
use think\Model;
class User extends Model
{
    public function index()
    {   
       
    }
 

    public function getUserInfo($phone)
    {    
           $data = [
              'zhuangtai'=>1,
              'mobile'=>$phone,
           ];
           return $userinfo = model('User')
                              ->field('uid,token,username,zhanghao,jifen,mobile,vip,userleixing')
                              ->where($data)
                              ->find(); 
    }

                
    public function insert($username,$zhanghao,$password,$phone)
    {   
     //return $token = mt_rand(111111,999999);
      $data = [
          'username'=>$username,
          'zhanghao'=>$zhanghao,
          'password'=>$password,
           'mobile'=>$phone,
           'vip'=>0,
           'jifen'=>0,
           'zhuangtai'=>1,
           'status'=>1,
           'token'=>mt_rand(111111,999999),
           'userleixing'=>0
      ];
       return $count=model('user')->insertGetId($data);
    }

    public function updateJF($token,$add,$reduce)
    {
        $res = model('user')->where('token',$token)->find();
            if($reduce!=0){ 
             $count = model('user')->where('token',$token)->update(['jifen'=>$res->jifen+$add-$reduce*10]);
            }else{
               $count = model('user')->where('token',$token)->update(['jifen'=>$res->jifen+$add]);
            }
        if ($count) {
           $result = model('user')->where('token',$token)->find();
            return $result->jifen;  
        }
    }

    public function getUser($token)
    {  
       $data = [
            'token'=>$token,
            'zhuangtai'=>1,
        ];
       $res = model('user')
                 ->field('username,zhanghao,password,mobile,vip,jifen,userleixing')
                 ->where($data)
                 ->find();
        return $res;
    }

    public function updateName($token,$name)
    {
      $count = model('user')->where('token',$token)->update(['username'=>$name]);
       return $count;
    }

    public function updateZH($token,$zhanghao)
    {
      $count = model('user')->where('token',$token)->update(['zhanghao'=>$zhanghao]);
      return $count;
    }
}